package com.ds.context;

import com.ds.esb.util.EsbFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class SpringMvcContextImpl extends JDSActionContext {
    public Map sessionMap = new HashMap();

    public Map contextMap = new HashMap();

    public SpringMvcContextImpl() {

    }

    public Object Par(String expression) {
        Object obj = null;
        try {
            obj = Par(expression, null);
        } catch (Exception e) {

        }
        return obj;
    }

    public Map getContext() {

        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            this.contextMap.putAll(request.getParameterMap());
        }

        return this.contextMap;
    }

    public Object getParams(String param) {
        Object value = null;

        if (RequestContextHolder.getRequestAttributes() != null) {

            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            if (requestAttributes != null) {
                HttpServletRequest request = requestAttributes.getRequest();
                if (request != null) {

                    value = request.getParameterMap().get(param);


                    if ((value == null || value.equals(""))
                            && (request.getSession() != null)
                            && (request.getSession().getAttribute(param) != null)) {

                        value = request.getSession().getAttribute(param).toString();
                    }

                    if (value == null || value.equals("")) {
                        Object obj = getContext().get(param);


                    }
                    if (value == null) {
                        value = request.getParameter(param);
                    }

                }
            }
        }

        if (value != null) {
            if (value.getClass().isArray()) {
                Object[] objs = (Object[]) getContext().get(
                        param);
                if (objs.length > 0 && objs[0] != null) {
                    value = objs[0].toString();
                }

            } else {
                value = value.toString();
            }
        }

        return value;
    }

    public String getIpAddr() {
        String ip = null;
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            ip = request.getHeader("X-Forwarded-For");

            String gatewayip = request.getHeader("gateway-ip");
            if ((gatewayip != null) && (!gatewayip.equals(""))) {
                ip = gatewayip;
            }

            if ((ip == null) || (ip.length() == 0)
                    || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if ((ip == null) || (ip.length() == 0)
                    || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if ((ip == null) || (ip.length() == 0)
                    || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if ((ip == null) || (ip.length() == 0)
                    || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if ((ip == null) || (ip.length() == 0)
                    || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getRemoteAddr();
            }
        }
        return ip;
    }

    public Map getSession() {
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            if (request != null) {
                HttpSession session = request.getSession(true);
                this.sessionMap = ((Map) session.getAttribute("sessionMap"));
                if (this.sessionMap == null) {
                    this.sessionMap = new HashMap();
                    session.putValue("sessionMap", this.sessionMap);
                }
            }
        }

        return this.sessionMap;
    }

    public String getSessionId() {
        String sessionid = null;
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            sessionid = (String) getParams("JSESSIONID");
            if ((sessionid == null) || (sessionid.equals("") || sessionid.equals("\"\""))) {
                //sessionid	=request.getSession(true).getId();
                sessionid = UUID.randomUUID().toString().toUpperCase();
                this.getSession().put("JSESSIONID", sessionid);
                request.getSession().setAttribute("JSESSIONID", sessionid);
            }
        }
        return sessionid;

    }

    public Object Par(String expression, Class clazz) {
        return Par(expression, clazz, null);
    }

    public Object Par(String expression, Class clazz, Object source) {
        Object o =EsbFactory.par(expression, this.contextMap,null,clazz);

        return o;
    }

    public Object getHttpRequest() {
        HttpServletRequest request = null;
        if (RequestContextHolder.getRequestAttributes() != null) {
            request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
        }

        return request;
    }

    public String getHost() {
        // TODO Auto-generated method stub
        return null;
    }
}
